<?php
class ThemeEditor extends ObjectModel 
{
    public $id;
    public $id_cms;
    public $active;
	public $date_add;
    public $date_upd;
    
    public static $definition = array(
        'table' => 'pt_theme_editor',
        'primary' => 'id_editor',
        'fields' => array(
            'id_cms' => array(
                'type' => self::TYPE_INT,
                'required' => true, 
                'validate' => 'isUnsignedId'
            ),
            'active' => array(
                'type' => self::TYPE_BOOL,
                'required' => true
            ),
            'date_add' =>  array(
                'type' => self::TYPE_DATE, 
                'validate' => 'isDateFormat'
            ),
			'date_upd' => array(
                'type' => self::TYPE_DATE,
                'validate' => 'isDateFormat'
            )
        )
    );
    
    public function getCMSName ()
    {
        $cms = new PrestathemeCms ($this->id_cms, Context::getContext()->language->id);
        if ($cms)
            return $cms->getCMSName();
        else
            return NULL;
    }
    
    public static function getEditors ($active = true)
    {
        $sql = '
            SELECT e.*, CONCAT(c.`code`, \' \', c.`version`) AS fullname 
            FROM `' . _DB_PREFIX_ . 'pt_theme_editor` e
            LEFT JOIN `' . _DB_PREFIX_ . 'pt_cms` c ON (e.`id_cms` = c.`id_cms`)
            '. ($active ? ' where e.`active` = 1' : '');
        if ($rows = Db :: getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($sql)) 
        {
            return $rows;
        }
        return array();
    }
}